﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.GuaZhiDuanLianService;
using EHR.Common;
using System.Collections.ObjectModel;

namespace CommunitySystem.PageView
{
    public partial class JYWindow : ChildWindow
    {
        private Emp_JYModel entity = new Emp_JYModel();
        private GuaZhiDuanLianServiceClient client = new GuaZhiDuanLianServiceClient();
        private bool IsAdd = true;
        public JYWindow()
        {
            InitializeComponent();

            this.Title = "新增人员借用信息";
            entity.Status = "借用中";
            this.DataContext = entity;
            IsAdd = true;
        
        }

       
        public JYWindow(Emp_JYModel model)
        {
            InitializeComponent();
            this.btnSel.Visibility = Visibility.Collapsed;
            this.Title = "编辑人员借用信息";
            entity = model;
            this.DataContext = model;
            IsAdd = false;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (entity.TLDate == null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择讨论时间!");
                return;
            }
            if (SelectItems == null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择借调人员!");
                return;
            }
            if (string.IsNullOrEmpty(entity.BeginDate))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择开始借调时间!");
                return;
            }
            if (string.IsNullOrEmpty(entity.NewDW))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择借调的单位!");
                return;
            }
            if (string.IsNullOrEmpty(entity.NewKS))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("科室不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(entity.NewZW))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("职务不能为空!");
                return;
            }
            if (!string.IsNullOrEmpty(entity.BeginDate) && Convert.ToDateTime(entity.BeginDate) < entity.TLDate)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("借调的开始时间不能早于讨论时间!");
                return;
            }
            entity.CreateDate = DateTime.Now;
            entity.CreateUser = Session.LoginUser.LoginName;
            client.SaveJYCompleted += new EventHandler<SaveJYCompletedEventArgs>(client_SaveJYCompleted);
            client.SaveJYAsync(entity,IsAdd);
        }

        void client_SaveJYCompleted(object sender, SaveJYCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功！");

            else
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存失败！");

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        ObservableCollection<EmpbasicService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
        private void btnSel_Click(object sender, RoutedEventArgs e)
        {
            SelOrgSelEmpNameWindow wind = new SelOrgSelEmpNameWindow(SelectItems);
            wind.Show();
            wind.Closed += (sender1, e1) =>
            {
                if (wind.DialogResult == true && wind.SelectItems != null)
                {
                    SelectItems = new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
                    foreach (var emp in wind.SelectItems)
                    {
                        if (SelectItems.Where(c => c.ID == emp.ID).Count() <= 0)
                        {
                            SelectItems.Add(new EmpbasicService.Emp_BasicInfoModel() { ID = emp.ID, Name = emp.Name, Birthday = emp.Birthday, PositionName = emp.PositionName, OrgName = emp.OrgName, Gender = emp.Gender,DutyName=emp.DutyName });
                        }
                        else
                        {
                            //  SelectItems.Remove(emp);
                        }
                    }
                    //   lbDetail.ItemsSource = SelectItems;

                    if (SelectItems.Count > 0)
                    {
                        var selEmp = SelectItems.First();
                        entity.EID = selEmp.ID;
                        entity.Name = selEmp.Name;
                        entity.OldKS = selEmp.PositionName;
                        entity.OldZW = selEmp.DutyName;

                    }
                }
            };

        }
    }
}

